package com.zb.order.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.entity.AccounLoginInfo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjWorkOrderByFieldService;
import com.zb.utils.TokenUtil;
import com.zb.vo.WorkOrderByFieldVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 工单列表排序字段表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2024-01-24
 */
@Api(tags = "工单列表字段排序类接口")
@RestController
@RequestMapping("/orderBy/field")
public class ZjWorkOrderByFieldController {
    @Resource
    private ZjWorkOrderByFieldService workOrderByFieldService;

    @ApiOperation("新增或修改工单列表排序字段")
    @PostMapping("/save/{type}")
    public Result save(HttpServletRequest request, @PathVariable @ApiParam(value = "列表排序类型：1-工单列表，2-存档列表，3-结算列表") Integer type, @RequestBody List<WorkOrderByFieldVo> fieldVos) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "列表排序类型参数不合法");
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        workOrderByFieldService.save(loginInfo, type, fieldVos);
        return Result.success("更新工单列表排序字段成功");
    }

    @ApiOperation("获取工单列表排序初始化字字段")
    @GetMapping("/getOrderTableInitOrderByField")
    public Result getOrderTableInitOrderByField() {
        return Result.success("获取工单列表排序初始化字字段成功").data(PageUtil.rows(workOrderByFieldService.getOrderTableInitOrderByField()));
    }

    @ApiOperation("获取工单列表排序字段设置")
    @GetMapping("/getOrderTableOrderByField/{type}")
    public Result getOrderTableOrderByField(HttpServletRequest request, @PathVariable @ApiParam(value = "列表排序类型：1-工单列表，2-存档列表，3-结算列表") Integer type) {
        Assert.isTrue(Pattern.matches("^(1|2|3)$", type.toString()), "列表排序类型参数不合法");
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        return Result.success("获取工单列表排序字段设置成功").data(PageUtil.rows(workOrderByFieldService.getOrderTableOrderByField(loginInfo, type)));
    }

}
